﻿
#include "libstring.h"


void string2others(string str)
{
    //const size_t str_length = str.length();

    // string --> const char*
    printf_s("string --> const char*\n");
    const char* constc = nullptr;                   //初始化const char*类型，并赋值为空
    constc = str.c_str();
    printf_s("%s --> %s\n", str.c_str(), constc);

    // string --> char*
    printf_s("string --> char*\n");
    char* c_pointer = nullptr;                      //初始化char*类型，并赋值为空
    c_pointer = const_cast<char*>(constc);          //const char*类型转char*类型
    printf_s("%s --> %s\n", str.c_str(), c_pointer);

    // string --> char 只适用于单个字符
    //cout << "string --> char" << endl;
    //char c = *const_cast<char*>(constc);

    // string --> char[]
    printf_s("string --> char[]\n");
    const size_t a = 20;
    char arrc[a] = { 0 };                           //初始化char[]类型，并赋值为空
    for (size_t i = 0; i < str.length(); i++)       //string类型转char[]类型
    {
        arrc[i] = str[i];
    }
    printf_s("%s --> %s\n", str.c_str(), arrc);  //打印char[]类型数据
}


void constchar2other(const char* constc)
{
    const size_t str_length = strlen(constc);

    //const char* --> string
    printf_s("const char* --> string\n");
    string str;                                         //初始化string类型
    str = constc;                                       //const char*类型转string类型
    printf_s("%s --> %s\n", constc, str.c_str());

    //const char* --> char*
    printf_s("const char* --> char*\n");
    char* c_pointer = nullptr;                          //初始化char*类型
    c_pointer = const_cast<char*>(constc);              //const char*类型转char*类型
    printf_s("%s --> %s\n", constc, c_pointer);

    //const char* --> char[]
    printf_s("const char* --> char[]\n");
    char arrc[20] = { 0 };                              //初始化char[]类型，并赋值为空 长度length
    strncpy_s(arrc, constc, str_length);                //const char*类型转char[]类型
    printf_s("%s --> %s\n", constc, arrc);
}


void char2other(char* c)
{
    //const size_t str_length = strlen(c);

    //char* --> string
    printf_s("char* --> string\n");
    string str;                             //初始化string类型
    str = c;                                //char*类型转string类型
    printf_s("%s --> %s\n", c, str.c_str());

    //char* --> const char*
    printf_s("char* --> const char*\n");
    const char* constc = nullptr;     //初始化const char* 类型，并具体赋值
    constc = c;                       //char*类型转const char* 类型
    printf_s("%s --> %s\n", c, constc);

    //char*转char[]
    printf_s("char* --> char[]\n");
    char arrc[20] = { 0 };           //初始化char[] 类型，并具体赋值
    strncpy_s(arrc, c, 20);             //char*类型转char[] 类型
    printf_s("%s --> %s\n", constc, arrc);
}


void charlist2other(char arrc[])
{
    //char[] --> string
    printf_s("char[] --> string\n");
    string str;                          //初始化string
    str = arrc;                                //char[]类型转string类型
    printf_s("%s --> %s\n", arrc, str.c_str());

    //char[] --> const char*
    printf_s("char[] --> const char*\n");
    const char* constc = nullptr;              //初始化const char*
    constc = arrc;                             //char[]类型转const char* 类型
    printf_s("%s --> %s\n", arrc, constc);

    //char[] --> char*
    printf_s("char[] --> char*\n");
    char* c = nullptr;                          //初始化char*
    c = arrc;                                  //char[]类型转char*类型
    printf_s("%s --> %s\n", arrc, c);
}


void Str2Num()
{
    string s = "0xffff";
    int tmp1 = 0;
    sscanf_s(s.c_str(), "%x", &tmp1);
    cout << s << " " << tmp1 << endl;
    //size_t tmp2 = 0;  //long
    //tmp2 = strtol(s.c_str(), NULL, 16);
    //cout << s << " " << tmp2 << endl;

    string a = to_string(2.144);
    cout << a << " " << 2.144 << endl;

    string s_int = "1555";
    string s_float = "2.1444";

    size_t t1 = atoi(s_int.c_str());		// int
    cout << s_int << " " << t1 << endl;
    size_t t2 = atol(s_int.c_str());		// long int 
    cout << s_int << " " << t2 << endl;
    //long long atoll(const char* nptr);	// long long int
    //long long atoq(const char* nptr);		// atoq() is an obsolete name for atoll()

    double t3 = atof(s_float.c_str());
    cout << s_float << " " << t3 << endl;
}


/*****************************************************************
* Function : string strip(const char* src, char flag)
* Description: 删除字符串前后的特定字符，类似python中的strip()
* Calls:
* Called By:
* Input:  @ const char* src : 待检查的字符串
* 		   @ char flag ： 需要删除的特定字符
* Output:
* Return: @ string dst : 处理后的字符串
* Others:
*****************************************************************/
string Strip(const char* src, char flag)
{
    char* strIn = const_cast<char*>(src);
    char strOut[256];
    size_t i = 0;
    size_t j = strlen(strIn) - 1;
    while (strIn[i] == flag)
        ++i;
    while (strIn[j] == flag)
        --j;
    strncpy_s(strOut, strIn + i, j - i + 1);
    strOut[j - i + 1] = '\0';
    string dst = strOut;
    return dst;
}



/*****************************************************************
* Function : wstring String2WString(const string& str)
* Description: 将字符串（string）转换为宽字符（const wstring）
* Calls:
* Called By:
* Input:  @ const string& str : 待转换的字符串
* Output:
* Return: @ wstring result : 处理后的字符串
* Others:
*****************************************************************/
wstring String2WString(const string &str)
{
    printf_s("String --> WString\n");
    //wstring wst(L"hello");
    _bstr_t t = str.c_str();
    wchar_t* pwchar = (wchar_t*)t;
    wstring result = pwchar;
    // printf("%ws\n", a);
    return result;
}


/*****************************************************************
* Function : string WString2String(const wstring& wstr)
* Description: 将宽字符（const wstring）转换为字符串（string）
* Calls:
* Called By:
* Input:  @ const wstring& wstr : 待转换的宽字符串
* Output:
* Return: @ string result : 处理后的字符串
* Others:
*****************************************************************/
string WString2String(const wstring &wstr)
{
    cout << "WString --> String" << endl;
    _bstr_t t = wstr.c_str();
    char* pchar = (char*)t;
    string result = pchar;
    return result;
}


/*****************************************************************
* Function : CString String2CString(const string& str)
* Description: 将String转换CString字符串
* Calls:
* Called By:
* Input:  @ CString cstr : 待转换的CString字符串
* Output:
* Return: @ string result : 处理后的字符串
* Others:
*****************************************************************/
CString String2CString(const string& str)
{
    cout << "String --> CString" << endl;
    CString result;
    for (size_t i = 0; i < str.length(); i++)
    {
        result += str[i];
    }
    //printf_s("%ls", result);
    return result;
}


/*****************************************************************
* Function : string CString2String(CString cstr)
* Description: 将CString转换字符串（string）
* Calls:
* Called By:
* Input:  @ CString cstr : 待转换的CString字符串
* Output:
* Return: @ string result : 处理后的字符串
* Others:
*****************************************************************/
string CString2String(const CString &cstr)
{
    cout << "CString --> String" << endl;
    string result(cstr.GetLength(), 'e');
    for (size_t i = 0; i < cstr.GetLength(); i++)
    {
        result[i] = (char)cstr[i];
    }
    return result;
}


/*****************************************************************
* Function : wstring CString2WString(CString str)
* Description: 将CString转换宽字符串（wstring）
* Calls:
* Called By:
* Input:  @ CString cstr : 待转换的CString字符串
* Output:
* Return: @ string wstr : 处理后的宽字符串
* Others:
*****************************************************************/
wstring CString2WString(CString cstr)
{
    cout << "CString --> WString" << endl;
    wstring wstr = (LPCWSTR)CStringW(cstr);
    return wstr;
}


/*****************************************************************
* Function : CString WString2CString(const wstring& wstr)
* Description: 将宽字符串（wstring）转换为CString
* Calls:
* Called By:
* Input:  @ string wstr : 待转换的wstring字符串
* Output:
* Return: @ CString cstr : 处理后的CString字符串
* Others:
*****************************************************************/
CString WString2CString(const wstring& wstr)
{
    cout << "CString --> WString" << endl;
    CString cstr = wstr.c_str();
    // printf_s("%ls", cs);
    return cstr;
}


/*****************************************************************
* Function : char* CString2Char(CString cstr)
* Description: 将CString转换char*
* Calls:
* Called By:
* Input:  @ CString cstr : 待转换的宽字符串
* Output:
* Return: @ char* result : 处理后的字符串
* Others:
*****************************************************************/
char* CString2Char(CString cstr)
{
    cout << "Cstring --> char *" << endl;
    char* result = (LPSTR)(LPCTSTR)cstr;
    return result;
}


/*****************************************************************
* Function : char* CString2Char(CString cstr)
* Description: 将CString转换char*
* Calls:
* Called By:
* Input:  @ CString cstr : 待转换的宽字符串
* Output:
* Return: @ char* dst : 处理后的字符串
* Others:
*****************************************************************/
//char* THCAR2Char(TCHAR* tchStr)
//{
//    cout << "TCHAR* --> char *" << endl;
//    size_t iLen = 2 * wcslen(tchStr); //CString,TCHAR汉字算一个字符，因此不用普通计算长度 
//    char* chRtn = new char[iLen + 1];
//    wcstombs(chRtn, tchStr, iLen + 1); //转换成功返回为非负值 
//    return chRtn;
//}


/*****************************************************************
* Function : wchar_t* chr2wch(const char* buffer)
* Description: 将字符串（string）转换TCHAR *
* Calls:
* Called By:
* Input:  @ const wstring& ws : 待转换的宽字符串
* Output:
* Return: @ string dst : 处理后的字符串
* Others: wBuf 申明为指针即可
*****************************************************************/
wchar_t* Char2WCh(const char* buffer)
{
    cout << "char* --> wchar_t *" << endl;
    size_t len = strlen(buffer);
    size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, len, NULL, 0);
    wchar_t* wBuf = new wchar_t[wlen + 1];
    MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, len, wBuf, wlen);
    return wBuf;
}


/*****************************************************************
* Function : LPWSTR ConvertCharToLPWSTR(const char* szString)
* Description: const char *转LPWSTR
* Calls:
* Called By:
* Input:  @ const char* szString : 待转化的const char *类型字符串
* Output:
* Return: @ LPWSTR lpszPath : 转化后的LPWSTR类型字符串
* Others:
*****************************************************************/
LPWSTR ConvertCharToLPWSTR(const char* szString)
{
    size_t dwLen = strlen(szString) + 1;
    size_t nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度
    LPWSTR lpszPath = new WCHAR[dwLen];
    MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);
    return lpszPath;
}



/*****************************************************************
* Function : char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
* Description: LPWSTR转char*
* Calls:
* Called By:
* Input:  @ LPWSTR lpwszStrIn : 待转化的LPWSTR类型
* Output:
* Return: @ LPSTR pszOut : 转化后的char*类型
* Others:
*****************************************************************/
char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
{
    LPSTR pszOut = NULL;
    try
    {
        if (lpwszStrIn != NULL)
        {
            size_t nInputStrLen = wcslen(lpwszStrIn);

            // Double NULL Termination  
            size_t nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0);
            nOutputStrLen = nOutputStrLen + 2;

            pszOut = new char[nOutputStrLen];

            if (pszOut)
            {
                memset(pszOut, 0x00, nOutputStrLen);
                WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
            }
        }
    }
    catch (std::exception e)
    {
        printf_s("%s", e.what());
    }
    return pszOut;
}



/*****************************************************************
* Function : string TCHAR2STRING(TCHAR* str)
* Description: TCHAR转string
* Calls:
* Called By:
* Input:  @ TCHAR* str : 待转化的TCHAR*类型字符串
* Output:
* Return: @ string strstr : 转化后的string类型字符串
* Others:
*****************************************************************/
string TCHAR2STRING(TCHAR* str)
{
    string strstr;
    try
    {
        size_t iLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
        char* chRtn = new char[iLen * sizeof(char)];
        WideCharToMultiByte(CP_ACP, 0, str, -1, chRtn, iLen, NULL, NULL);
        strstr = chRtn;
    }
    catch (exception e)
    {
        cout << e.what() << endl;
    }
    return strstr;
}



/*****************************************************************
* Function : LPCWSTR stringToLPCWSTR(string& orig)
* Description: string转LPCWSTR
* Calls:
* Called By:
* Input:  @ string& orig : 待转化的string类型字符串
* Output:
* Return: @ wchar_t* wcstring : 转化后的LPCWSTR类型字符串
* Others:
*****************************************************************/
LPCWSTR stringToLPCWSTR(string& orig)
{
    wchar_t* wcstring = 0;
    try
    {
        //size_t origsize = orig.length() + 1;
        //const size_t newsize = 100;
        size_t convertedChars = 0;
        if (orig == "")
        {
            wcstring = (wchar_t*)malloc(0);
            mbstowcs_s(&convertedChars, wcstring, orig.length() + 1, orig.c_str(), _TRUNCATE);
        }
        else
        {
            wcstring = (wchar_t*)malloc(sizeof(wchar_t) * (orig.length() - 1));
            mbstowcs_s(&convertedChars, wcstring, orig.length() + 1, orig.c_str(), _TRUNCATE);
        }
    }
    catch (exception e)
    {
        printf_s(e.what());
    }
    return wcstring;
}


/*****************************************************************
* Function : LPCWSTR stringToLPCWSTR(string orig)
* Description: 将字符串（string）转换LPCWSTR
* Calls:
* Called By:
* Input:  @ string orig : 待转换的字符串
* Output:
* Return: @ string dst : 处理后的字符串
* Others: LPCWSTR是一个指向unicode编码字符串的32位指针，所指向字符串是wchar型，而不是char型。
*****************************************************************/
LPCWSTR String2LPCWSTR(string orig)
{
    cout << "String --> LPCWSTR" << endl;
    size_t convertedChars = 0;
    wchar_t* wcstring = (wchar_t*)malloc(sizeof(wchar_t) * (orig.length() - 1));
    mbstowcs_s(&convertedChars, wcstring, orig.length() + 1, orig.c_str(), _TRUNCATE);
    return wcstring;
}
